package db_event

import (
	"errors"
	"fmt"
	"game_server/logic/db/mark"
	"game_server/model"
)

type (
	collectEvent struct {
	}
	collectCallback = func(player *model.Player, mark db_mark.Enum)
)

var Collect = new(collectEvent)

func (event *collectEvent) Verify(callback interface{}) (err error) {
	if _, ok := callback.(collectCallback); !ok {
		err = errors.New("not collect event callback function")
	}

	return
}

func (event *collectEvent) Call(callback interface{}, args []interface{}) (err error) {
	const ArgsCount = 2
	if len(args) < ArgsCount {
		err = fmt.Errorf("collect event callback need %d arg", ArgsCount)
		return
	}

	call := callback.(collectCallback)
	i := 0
	player := args[i].(*model.Player)
	i++
	mark := args[i].(db_mark.Enum)
	i++

	call(player, mark)

	return
}

type (
	saveEvent struct {
	}
	saveCallback = func(player *model.Player)
)

var Save = new(saveEvent)

func (event *saveEvent) Verify(callback interface{}) (err error) {
	if _, ok := callback.(saveCallback); !ok {
		err = errors.New("not save event callback function")
	}

	return
}

func (event *saveEvent) Call(callback interface{}, args []interface{}) (err error) {
	const ArgsCount = 1
	if len(args) < ArgsCount {
		err = fmt.Errorf("save event callback need %d arg", ArgsCount)
		return
	}

	call := callback.(saveCallback)
	i := 0
	player := args[i].(*model.Player)
	i++

	call(player)

	return
}
